package testJdk.io.object_stream;

import lombok.Data;

import java.io.*;

/**
 * @Name : com.io.object_stream.ObjectSaver
 * @Description : 描述
 * Created by cyd on 2020-12-23 11:56:16
 */
public class ObjectSaver {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 创建对象输出流
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:/object.obj"));
        // 创建对象
        Customer customer = new Customer("tom", 20);
        // 序列化对象到磁盘
        out.writeObject(customer);
        // 创建对象输入流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/object.obj"));
        // 反序列化对象到程序
        Object o = ois.readObject();
        //关闭流
        out.close();
        ois.close();
        //打印对象
        System.out.println(o);
    }

}

@Data
class Customer implements Serializable{
    private String name;
    private int age;

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }
}